package xzr4;

import javax.swing.*;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import java.awt.*;

public class ColorTest {
    private JFrame jf;
    private JLabel label1,label2,label3;
    private JSlider slider1,slider2,slider3;
    private  JTextField field1,field2,field3;
    private  JTextArea jta;

    public ColorTest() {
        jf = new JFrame("调色板测试");
        label1 = new JLabel("R:");
        label2 = new JLabel("G:");
        label3 = new JLabel("B:");
        slider1 = new JSlider(0,255,127);
        slider2 = new JSlider(0,255,127);
        slider3 = new JSlider(0,255,127);
        field1 = new JTextField("127");
        field2 = new JTextField("127");
        field3 = new JTextField("127");
        field1.setEditable(false);
        field2.setEditable(false);
        field3.setEditable(false);
        jta = new JTextArea(5,10);
        jta.setEditable(false);
        jta.setBackground(new Color(10, 172, 100));
        init();
        addEvenrHandler();

    }
    public void init(){
        JPanel jp1 = new JPanel();
        jp1.add(label1);
        jp1.add(slider1);
        jp1.add(field1);
        jp1.add(label2);
        jp1.add(slider2);
        jp1.add(field2);
        jp1.add(label3);
        jp1.add(slider3);
        jp1.add(field3);
        JPanel jp2 = new JPanel();
        jp2.add(jta);
        jf.add(jp1,BorderLayout.CENTER);
        jf.add(jp2,BorderLayout.EAST);
    }
    public void addEvenrHandler(){
        ChangeListener listener = new ChangeListener() {
            @Override
            public void stateChanged(ChangeEvent e) {
                int r = slider1.getValue();
                int g = slider2.getValue();
                int b = slider3.getValue();
                field1.setText(r+"");
                field2.setText(g+"");
                field3.setText(b+"");
                Color color = new Color(r,g,b);
                jta.setBackground(color);
            }
        };
        slider1.addChangeListener(listener);
        slider2.addChangeListener(listener);
        slider3.addChangeListener(listener);
    }
    public void showMe(){
        jf.setSize(400,180);
        jf.setVisible(true);
        jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
    public static void main(String[] args) {
        ColorTest colorTest = new ColorTest();
        colorTest.showMe();
    }
}
